# -*- coding: utf-8 -*-
#twisted nevow stuff
from twisted.python.util import sibpath
from twisted.internet import reactor
from nevow import athena, rend, inevow, static, loaders, tags as T
#from nevow.athena import LivePage, LiveElement, expose
from zope.interface import implements
# midtown stuff
from midtown.mtaxiom import *
# general python
import os

class FrontPage(rend.Page):
    implements(inevow.IResource)

    def __init__(self, webroot, mm):
        super(FrontPage, self).__init__()
        self.webroot = webroot
        self.mm = mm
        self.docFactory = loaders.xmlfile(self.webroot + '/FrontPage.html')

    def locateChild(self, ctx, segments):
        for seg in segments:
            print seg

        if (segments[0]):
            possible_file = os.path.join(self.webroot, '/'.join(segments))
            if(os.access(possible_file, os.R_OK)):
                return static.File(possible_file), ()

        if (segments[0] == 'join' and len(segments) > 1):
            return Join(self.webroot, self.mm), segments[1:]

        return self, ()

class Join(athena.LivePage):
#    docFactory = loaders.stan(T.html[
#        T.head(render=T.directive('liveglue')),
#        T.body(render=T.directive('LiveChat'))])

    def __init__(self, webroot, mm):
        super(Join, self).__init__()
        self.webroot = webroot
        self.mm = mm
        self.docFactory = loaders.xmlfile(self.webroot + '/join.html')

    def render_LiveChat(self, ctx, data):
        f  = LiveChat(self.mm)
        f.setFragmentParent(self)
        return ctx.tag[f]

    def locateChild(self, ctx, segments):
        if(segments[0]):
            return super(Join, self).locateChild(ctx, segments)
        return self, ()

class LiveChat(athena.LiveElement):
    jsClass = u'midtown.LiveChat'

    #docFactory = loaders.stan(T.div(render=T.directive('liveElement'))[
    #        T.div(id='chatroom')
    #        ])

    docFactory = loaders.stan(T.div(render=T.directive('liveElement'))[
            T.input(type="submit", value="Push me", 
                    onclick='Nevow.Athena.Widget.get(this).clicked()')])

    def __init__(self, mm):
        #super(LiveChat, self).__init__(*args, **kwargs)
        #self.store = MidtownManager().store
        #for x in self.store.query(MtRoom, MtRoom.name = u'WebChat'):
        self.mm = mm
        for msg in self.mm.getAllMessages():
            print msg.body
        self.store = self.mm.store
        #for x in self.store.query(MtRoom):
        #    print 'KAZU: room name == ' + x.name

        #reactor.callLater(5, self.myEvent)

    def myEvent(self):
        print 'my event firing'
        self.callRemote('echo', 12345)

    def echo(self, argument):
        print 'Echoing', argument
        return argument
    athena.expose(echo)

    def setUsername(self, username):
        print username
        self.userame = username
        msg = 'User' + username + ' has joined'
        self.room.wall(msg)
    athena.expose(setUsername)

    def say(self, msg):
        print msg
        self.room.tellEverybody(self, message)
    athena.expose(say)

